﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineShop.Models.ViewModels;
using OnlineShop.Models;
using System.Security.Cryptography;
using System.Text;
using PaymentHelper.Core.RelatedObject;
using PaymentHelper;

namespace OnlineShop.Common
{
    public static class CommonUtils
    {
        public static string FormatUSD(this double price)
        {
            return "$" + price.ToString("0,00"); 
        }

        public static string FormatUSD(this decimal price)
        {
            return "$" + price.ToString("0,00");
        }

        public static string Encrypt(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return string.Empty;
            var md5 = new MD5CryptoServiceProvider();
            byte[] valueArray = Encoding.ASCII.GetBytes(value);
            valueArray = md5.ComputeHash(valueArray);
            var sb = new StringBuilder();
            for (int i = 0; i < valueArray.Length; i++)
                sb.Append(valueArray[i].ToString("x2").ToLower());
            return sb.ToString();
        }

        public static ProductModel ToModel(this Product product, int quantity = 1)
        {
            ProductModel productModel = new ProductModel()
            {
                Id = product.Id,
                Image = product.Image,
                Name = product.Name,
                Price = product.Price.Value,
                Quantity = quantity
            };

            return productModel;
        }

        public static OrderItem ToOrderItem(this ProductModel product)
        {
            return new OrderItem()
            {
                Id = product.Id.ToString(),
                Name = product.Name,
                Price = new decimal(product.Price),
                Quantiy = product.Quantity,
                ShortDescription = "",
            };
        }

        public static CategoryModel ToModel(this Category category)
        {
            return new CategoryModel() { 
                Id = category.Id,
                Name = category.Name,
                NumProduct = category.Products.Count()
            };
        }

        public static PaymentInfoModel ToPaymentInfoModel(this IPaymentMethod method)
        {
            return new PaymentInfoModel() { 
                Fee =  method.GetHandlingFee(),
                FriendlyName = method.Descriptor.FriendlyName,
                SystemName = method.Descriptor.SystemName,
                ShortDescription = method.Descriptor.Description
            };
        }

        public static Address ToAddress(this UserInfo userInfo)
        {
            return new Address() { 
                Address1 = userInfo.Address1,
                Address2 = userInfo.Address2,
                City = userInfo.City,
                Company = "",
                Country = userInfo.CountryCode,
                Email = userInfo.User.Email,
                FaxNumber = "",
                FirstName = userInfo.FistName,
                LastName = userInfo.LastName,
                PhoneNumber = userInfo.PhoneNumber,
                StateProvince = userInfo.StateProvince,
                ZipPostalCode = userInfo.ZipPostalCode
            };
        }
    }
}